package controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.render.RenderFactory;
import interceptor.UserLoginInterceptor;
import model.User;
import service.IPurchasesService;
import service.IUserService;
import service.impl.PurchasesServiceImpl;
import service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ykj on 16/1/11.
 */
public class UserController extends BaseController {

    public void addDialog(){
        render("/admin/userAddDialog.jsp");
    }

    public void updateDialog() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IUserService userService = new UserServiceImpl();
        User user = (User) userService.findById(id);
        setAttr("user",user);
        render("/admin/userUpdateDialog.jsp");
    }

    public void save() {
        User user = getModel(User.class);
        user.setAsset(10000d);
        user.setEarnings(0d);
        IUserService userService = new UserServiceImpl();
        User result = (User) userService.save(user);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void update() {
        User user = getModel(User.class);
        IUserService userService = new UserServiceImpl();
        User result = (User) userService.update(user);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void delete() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IUserService userService = new UserServiceImpl();
        boolean result = userService.delete(id);
        renderDelResult(result);
    }

    public void getColumns() {
        List<String> fields = new ArrayList<String>() {{
            add("id");
            add("phone");
            add("pwd");
            add("name");
            add("id_card");
            add("email");
            add("asset");
            add("earnings");
        }};
        List<String> titles = new ArrayList<String>() {{
            add("标识");
            add("手机号");
            add("密码");
            add("真实名字");
            add("身份证");
            add("邮箱");
            add("用户资产");
            add("收益");
        }};
        renderColumns(fields, titles);
    }

    public void users() {
        IUserService userService = new UserServiceImpl();
        renderData(userService);
    }

    public void login(){
//        String returnUrl = getPara("returnUrl");
//        if(returnUrl!=null){
//            setAttr("returnUrl",returnUrl);
//        }else{
//            setAttr("returnUrl","index");
//        }
        render("/front/login.jsp");
    }
    public void loginService(){
        String username = getPara("username");
        String pwd = getPara("password");
        IUserService userService = new UserServiceImpl();
        User user = userService.login(username,pwd);
        Map map = new HashMap();
        map.clear();
        if(user == null){
            map.put("loginSuccess",false);
            map.put("message","用户或密码错误");
            renderJson(map);
        }else{
            getSession().setAttribute("isLogin",true);
            getSession().setAttribute("user",user);
            map.put("returnUrl","/index");
            map.put("loginSuccess",true);
            map.put("message","登陆成功");
            renderJson(map);
        }
    }

    public void logout(){
        getSession().removeAttribute("isLogin");
        getSession().removeAttribute("user");
        redirect("/index");
    }

    public void register(){
        render("/front/register.jsp");
    }

    public void registerService(){
        User user = getModel(User.class);
        user.setAsset(1000000d);
        user.setEarnings(0d);
        IUserService userService = new UserServiceImpl();
        user = (User) userService.save(user);
        if(user!=null){
            getSession().setAttribute("isLogin",true);
            getSession().setAttribute("user",user);
            redirect("/index");
        }else{
            keepModel(User.class);
            setAttr("errorTip","已存在该用户");
            render("/front/register.jsp");
        }
    }

    @Before(UserLoginInterceptor.class)
    public void info(){
        render("/front/userInfo.jsp");
    }

    public void infoDetail(){
        User user = (User) getSession().getAttribute("user");
        if(user==null){
            renderError(401);
            return;
        }
        IUserService userService = new UserServiceImpl();
        User uData = (User) userService.findById(user.getId());
        setAttr("user",uData);
        render("/front/infoDetail.jsp");
    }

    public void investmentInfo(){
        render("/front/investmentInfo.jsp");
    }

    public void allPurchases(){
        User user = (User) getSession().getAttribute("user");
        if(user==null){
            renderError(401);
            return;
        }
        String pageStr = getPara("page");
        String rowsStr = getPara("rows");
        int page = pageStr!=null?Integer.parseInt(pageStr):1;
        int rows = rowsStr!=null?Integer.parseInt(rowsStr):10;
        IPurchasesService purchasesService = new PurchasesServiceImpl();
        Page purchases = purchasesService.findAllByUserId(user.getId(), page, rows);
        setAttr("purchases",purchases);
        render("/front/allPurchases.jsp");
    }
}
